<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
		<title>Finalove-App-newFriends</title>
		<link rel="stylesheet" href="../../resource/font/css/font-awesome.min.css">
		<link rel="stylesheet" href="../../resource/css/mint.css">
		<link rel="stylesheet" href="../../resource/css/style.css">
		<script src="../../resource/js/vue.js"></script>
		<script src="../../resource/js/mint.min.js"></script>
		<script src="../../resource/js/jquery.min.js"></script>
		<style type="text/css">
			#bcid{
				width: 100%;
				height: 100%;
				background-color: #000;
				position: absolute;
			}
		</style>
	</head>
	<body>
		<div id="main">
			<div id="bcid"></div>
		</div>
	</body>
	<script type="text/javascript">
		var main = new Vue({
			el:"#main",
			data:{
				searchText:"",
			},
			mounted:function() {
				this.init();
			},
			methods:{
				init() {
					document.addEventListener('plusready',function () {
						plus.navigator.setFullscreen(true);
						plus.key.addEventListener('backbutton', function() {
							plus.webview.currentWebview().close();
							return false;
						}, false);
						// 扫描
						main.scan();
						// 获取信息
						main.userInfo = {
							user_id:plus.storage.getItem("user_id"),
							user_name:plus.storage.getItem("user_name"),
							nick_name:plus.storage.getItem("nick_name"),
							email:plus.storage.getItem("email"),
							user_head:plus.storage.getItem("user_head"),
						}
					});
				},
				closeView() {
					plus.webview.currentWebview().close();
				},
				scan() {
					try{
						var filter;
						//自定义的扫描控件样式
						var styles = {frameColor: "#409EFF",scanbarColor: "#409EFF",background: ""}
						//扫描控件构造
						scan = new plus.barcode.Barcode('bcid',filter,styles);
						scan.onmarked = function(type, result) {
							// alert( result );
							plus.nativeUI.showWaiting("正在努力识别中...");
							var checkurl = result.split("?")[0];
							var url = "https://www.finalove.top/index/index/applyByQRcode";
							if(checkurl == url){
								result += "&from_id="+main.userInfo.user_id;
								$.get(result,function(res) {
									plus.nativeUI.closeWaiting();
									var scanPage = plus.webview.currentWebview();
									var homePage = scanPage.opener();
									if(res.code == 0){
										homePage.evalJS("main.$toast({message:'"+res.msg+"',position:'bottom',duration:5000})");
									}else{
										plus.webview.close("newFriendDetails");
										var newView = plus.webview.create('../friend/new_friend_details.html','newFriendDetails',{},{
											param:{
												id:res.data.id,
												user_name:res.data.user_name,
												nick_name:res.data.nick_name,
												email:res.data.email,
												head:res.data.head
											}
										});
										plus.webview.show(newView,'slide-in-right',200);
									}
									main.closeView();
								});
							}else{
								plus.nativeUI.closeWaiting();
								alert("识别结果不符，无法添加好友；\n识别结果：\n"+result);
								main.closeView();
							}
						} 
						scan.onerror = function(e) {
							main.$toast(e);
						}
						scan.start();
					}catch(e){
						alert("出现错误啦:\n"+e);
					}
				}
			}
		})
	</script>
</html>
